
<script type="text/javascript" src="../js/jquery-easyui-1.3.5/jquery.min.js" charset="UTF-8"></script>
 <script type="text/javascript" src="../js/jquery-easyui-1.3.5/jquery.easyui.min.js" charset="UTF-8"></script>
 <link rel="stylesheet" href="../js/jquery-easyui-1.3.5/themes/default/easyui.css" type="text/css"></link>
 <link rel="stylesheet" href="../js/jquery-easyui-1.3.5/themes/icon.css" type="text/css"></link>
 <link rel="stylesheet" href="../css/main.css" type="text/css"></link>
 <script type="text/javascript" src="../js/mine.js" charset="UTF-8"></script>
 <script type="text/javascript" src="../js/jquery-easyui-1.3.5/locale/easyui-lang-zh_CN.js" charset="UTF-8"></script>
<script type="text/javascript" src="../js/validate.js" charset="UTF-8"></script> 
<script type="text/javascript" charset="UTF-8">
	var teacherDatagrid;
	var adminTeacherLayout;
	var adminTeacherForm;
	var editRow;
	var sexData = [{
		"id":"male",
		"text":"男"
	},{
		"id":"female",
		"text":"女",
	}];
	
	$(function(){
		editRow = undefined;
		
		adminTeacherLayout = $('#adminTeacherLayout').layout();
		adminTeacherForm = $('#adminTeacherForm').form();
		teacherDatagrid = $('#teacherDatagrid').datagrid({
			url:'../servlet/GetTeacherServlet',
			//title:'教师信息',
			//iconCls:'icon-save',
			fitColumns:true,
			nowrap:false,
			striped:true,
			idField:'tno',
			pagination:true,
			pageSize:10,
			pageList:[5,10,15],
			fit:true,
			border:false,
			sortName:'tno',
			sortOrder:'desc',
			//showFooter:true,
			columns:[[{
				field:'cut',
				checkbox:true
			},{
				title:'工号',
				field:'tno',
				width:100,
				sortable:true,
				editor:{
					type:'numberbox',
					options:{
						required:true,
						validType:'nums'	
					}
				}
			},{
				title:'姓名',
				field:'tname',
				width:100,
				sortable:true,
				editor:{
					type:'validatebox',
					options:{
						required:true,
					}
				}
			},{
				title:'性别',
				field:'sex',
				width:100,
				sortable:true,
				formatter:function(value,rowData,rowIndex){
					 for(var i=0; i<sexData.length; i++){  
			                if (sexData[i].id == value) {
			                	return sexData[i].text;  
			                }
			            }  
		            return value;  
				},
				editor:{
					type:'combobox',
					options:{
						required:true,
						data:sexData,
						valueField:'id',
						textField:'text',
						editable:false
					}
				}
			},{
				title:'发布项目数',
				field:'proNum',
				width:100
			}]],toolbar:[{
				text:'增加',
				iconCls:'icon-add',
				handler:function(){
					addTeacher();
				}
			},'-',{
				text:'删除',
				iconCls:'icon-cancel',
				handler:function(){
					deleteTeacher();
				}
			},'-',{
				text:'修改',
				iconCls:'icon-edit',
				handler:function(){
					modifyTeacher();
				}
			},'-',{
				text:'重置密码',
				iconCls:'icon-redo',
				handler:function(){
					resetPsw();
				}
			},'-',{
				text:'保存',  
				iconCls:'icon-save',
				handler:function(){					
						teacherDatagrid.datagrid('endEdit', editRow);		
				}
			},'-',{
				text:'取消',
				iconCls:'icon-undo',
				handler:function(){
					editRow = undefined;
					teacherDatagrid.datagrid('unselectAll');
					teacherDatagrid.datagrid('rejectChanges');
				}
			},'-',{
				text:'刷新',
				iconCls:'icon-reload',
				handler:function(){
					teacherDatagrid.datagrid('unselectAll');
					teacherDatagrid.datagrid('rejectChanges');
					teacherDatagrid.datagrid('load');
				}
			},'-'],
			onAfterEdit:function(rowIndex, rowData, changes) {
				console.info(rowData);
				var inserted = teacherDatagrid.datagrid('getChanges','inserted');
				console.info(inserted);
				var updated = teacherDatagrid.datagrid('getChanges','updated');
				console.info(updated);
				
				if(inserted.length > 0 || updated.length > 0) {
					var url = '';
					if(inserted.length > 0) {
						url = '../servlet/InsertTeacherServlet';
					} else if(updated.length > 0) {
						url = '../servlet/UpdateTeacherServlet';
					}
					
					$.ajax({
						url:url,
						data:rowData,
						type:'post',
						dataType:'json',
						success:function(r) {
							if(r && r.success) {
								teacherDatagrid.datagrid('acceptChanges');
								teacherDatagrid.datagrid('reload');
								$.messager.show({
									title:'提示',
									msg:r.msg,
								});
							} else {
								teacherDatagrid.datagrid('rejectChanges');
								$.messager.alert('出错啦',r.msg,'error');
							}
						}
					});
					
				} 
				
				editRow = undefined;
			},
			onRowContextMenu:function(e, rowIndex, rowData) {
				e.preventDefault();
				//$(this).datagrid('unselectAll');
				//$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left:e.pageX,
					top:e.pageY
				});
			}
		});
			
		$('#sex').combobox({
			data:[{
				"id":"all",
				"text":"全部",
			},{
				"id":"male",
				"text":"男"
			},{
				"id":"female",
				"text":"女",
			}],
			valueField:'id',
			textField:'text',
			editable:false
		});
			
	});
	
	addTeacher = function() {
		if(editRow == undefined) {
			teacherDatagrid.datagrid('unselectAll');
			teacherDatagrid.datagrid('addEditor',[{
				field:'tno',
				editor:{
					type:'validatebox',
					options:{
						required:true,
						validType:'nums'	
					}
				}
			},{
				field:'sex',
				editor:{
					type:'combobox',
					options:{
						required:true,
						data:sexData,
						valueField:'id',
						textField:'text',
						editable:false
					}
				}
			}]); 
			
			teacherDatagrid.datagrid('insertRow', {
				index:0,
				row:{
					tno:'',
					tname:'',
					sex:''
				}
			});
			
			teacherDatagrid.datagrid('selectRow', 0);
			teacherDatagrid.datagrid('beginEdit', 0);
			editRow = 0;
		} else {
			teacherDatagrid.datagrid('endEdit',editRow);
		}
	}
	
	deleteTeacher = function() {
		var rows = teacherDatagrid.datagrid('getSelections');
		var ids = [];
		var cantd = [];
		var m = '';
		var cantm = '';
		
		if(editRow == undefined) {
			if(rows.length > 0) {
				for(var i=0;i<rows.length; i++) {
					if(rows[i].proNum == '0' && rows[i].tno != '') {
						ids.push(rows[i].tno);
					} else {
						cantd.push(rows[i].tno);
						cantm += "<br/> [" + rows[i].tno + "&nbsp;&nbsp;&nbsp;&nbsp;" + rows[i].tname + "]";
					}
				}
				
				if(cantd.length > 0 && ids.length == 0) {//选择的数据都不能删除
					m = '选择的数据都不能删除.';
					$.messager.alert('提示',m,'warning');
				} else {
					if(cantd.length > 0 && ids.length > 0){//选择的数据部分不能删除
						m = '<div style="width:10px;margin:auto auto auto 45px;">选择的数据中:' + cantm + '<br/>还有项目信息,不能删除.<br>确定要删除选中的其余数据吗?</div>';
					} else if(cantd.length == 0 && ids.length > 0) {//选择的数据都可以删除
						m = '确定要删除所选择的教师吗?';
					}
				
					$.messager.confirm('提示',m,function(b) {
							if(b) {
								for(var i=0;i<rows.length; i++) {
									ids.push(rows[i].tno);
								}
								console.info(ids);
								$.ajax({
									url:'../servlet/DeletTeacherServlet',
									data:{
										ids:ids.join(',')//用,分隔
									},
									type:'post',
									dataType:'json',
									success:function(r) {
										if(r && r.success) {
											teacherDatagrid.datagrid('acceptChanges');
											teacherDatagrid.datagrid('load');
											teacherDatagrid.datagrid('unselectAll');
											teacherDatagrid.datagrid('rejectChanges');
											//console.info(teacherDatagrid.datagrid('getSelections'));
											$.messager.show({
												title:'提示',
												msg:r.msg
											});
										} else {
											$.messager.alert('出错啦',r.msg,'error');
										}
									}
								});
							}
					});
				}
			} else {
				$.messager.alert('提示','未选择任何数据.','warning');
			}
		} 
	}
	
	modifyTeacher = function() {
		var rows = teacherDatagrid.datagrid('getSelections');
		if(rows.length == 1 ) {
			if(editRow == undefined) {
				teacherDatagrid.datagrid('removeEditor', ['tno','sex']);
				
				var index = teacherDatagrid.datagrid('getRowIndex', rows[0]);
				if(rows[0].tno != '') {
					teacherDatagrid.datagrid('beginEdit', index);
					editRow = index;
					//teacherDatagrid.datagrid('unselectAll');
				} else {
					$.messager.alert('提示','空白数据不能进行修改.', 'warning');
				}
			} else {
				teacherDatagrid.datagrid('unselectAll');
				teacherDatagrid.datagrid('endEdit', editRow);
			}
		} else if(rows.length > 1) {
			$.messager.alert('提示','只能选择一行数据进行修改.','warning');
		} else {
			$.messager.alert('提示','未选择需要修改的数据.', 'warning');
		}
	}
	
	resetPsw = function() {
		var rows = teacherDatagrid.datagrid('getSelections');
		var ids = [];
		if(rows.length > 0) {
			for(var i=0;i<rows.length; i++) {
				ids.push(rows[i].tno);
			}
			
			if(ids[0] != '') {
				$.messager.confirm('提示','你确定要重置选中教师的密码吗?',function(b) {
					if(b) {
						$.ajax({
							url:'../servlet/ResetPswServlet',
							data:{
								ids:ids.join(','),    //用,分隔
								userRole:'teacher'
							},
							type:'post',
							dataType:'json',
							success:function(r) {
								if(r && r.success) {
									teacherDatagrid.datagrid('acceptChanges');
									teacherDatagrid.datagrid('load');
									teacherDatagrid.datagrid('unselectAll');
									teacherDatagrid.datagrid('rejectChanges');
									//console.info(teacherDatagrid.datagrid('getSelections'));
									$.messager.show({
										title:'提示',
										msg:r.msg
									});
								} else {
									$.messager.alert('提示',r.msg,'error');
								}
							}
						});
					}
				});
			} else {
				$.messager.alert('提示','空白数据不能进行操作.','warning');
			}
		} else {
			$.messager.alert('提示','未选择任何数据.','warning');
		}
	}
	
	searchTeacher = function() {
		teacherDatagrid.datagrid('load',serializeObject(adminTeacherForm));
	}
	
	cleanTeacher = function() {
		teacherDatagrid.datagrid('load',{});
		adminTeacherForm.find('input').val('');
	}
</script>

<div id="adminTeacherLayout" fit="true" border="false" >
		<div region="north" border="false" split="false" title="查询" style="margin:0 auto;height:55px;" >
			<form id="adminTeacherForm" class="datagrid-toolbar" style="margin:0 auto;">
				<table>
					<tr>
						<td><b>工号</b><input type="text" name="tno" class="easyui-numberbox" placeholder="您只能输入数字" /></td>
						<td><b>姓名</b><input type="text" name="tname"/></td>
						<td><b>性别</b><input type="text" name="sex" id="sex"/></td>
						<td>
							<a href="javascript:void(0);" class="easyui-linkbutton"  iconCls="icon-search" pain="false" onclick="searchTeacher();" ></a>
						</td>
						<td>	
							<a href="javascript:void(0);" class="easyui-linkbutton"  pain="false" onclick="cleanTeacher();" >清空</a>
						</td>
					</tr>
				</table>
			</fieldset>
			</form>
	    </div>
		    
	   	<div region="center" border="false" >
			<table id="teacherDatagrid" ></table>
		</div>
		
		<div id="menu" class="easyui-menu" style="width:120px;display:none;">
			<div onclick="addTeacher();" iconCls="icon-add" >增加</div>
			<div onclick="deleteTeacher();" iconCls="icon-remove" >删除</div>
			<div onclick="modifyTeacher();" iconCls="icon-edit" >修改</div>
			<div onclick="resetPsw();" iconCls="icon-edit" >重置密码</div>
		</div>
</div>

